use "*/Replication files/anes_timeseries_cdf.dta" , clear


set more off

svyset [pw=VCF0009z]
gen year = VCF0004
drop if year<1996
drop if year==2002

*Mode
gen mode = VCF0017

* Satisfaction with democracy
gen satisfaction = VCF9254 if year<2002
replace satisfaction = VCF9255 if year>2002
revrs satisfaction , replace

*Racial resentment
*Conditions Make it Difficult for Blacks to Succeed
gen racial_resentment1 = VCF9039	
recode racial_resentment1 (8=.) (9=.)
recode racial_resentment1 (1=5) (5=1) (4=2) (2=4)

*Blacks Should Not Have Special Favors to Succeed
gen racial_resentment2 = VCF9040	
recode racial_resentment2 (8=.) (9=.)

*Blacks Must Try Harder to Succeed
gen racial_resentment3 = VCF9041	
recode racial_resentment3 (8=.) (9=.)

*Blacks Gotten Less than They Deserve Over the Past Few Years
gen racial_resentment4 = VCF9042	
recode racial_resentment4 (8=.) (9=.)
recode racial_resentment4 (1=5) (5=1) (4=2) (2=4)
* Higher values represent more resentment
gen racial_resentment = (racial_resentment1 + racial_resentment2 + ///
	racial_resentment3 + racial_resentment4)/4
revrs racial_resentment , replace

* Blacks lazy
gen blackslazy = VCF9271

* Black termometer
gen blacktherm = VCF0206
recode blacktherm (97=100)
*White thermometer
gen whitetherm = VCF0207
recode whitetherm (97=100)
* Difference
gen diftherm = whitetherm-blacktherm

* Winner post
gen vote_post = VCF0704
replace vote_post = vote_post-1
replace vote_post = . if vote_post == 2
gen winner_post=vote_post
recode winner_post (1=0) (0=1) if year==2008
recode winner_post (1=0) (0=1) if year==2012

* Winner
gen vote = VCF0713
recode vote (1=0) (2=1) (3=.) (4=.) (9=.)
gen winner=vote
recode winner (1=0) (0=1) if year==2008
recode winner (1=0) (0=1) if year==2012
recode winner (1=0) (0=1) if year==1996

gen consistent = 1 if VCF0713==1 & VCF0704==1
replace consistent = 1 if VCF0713==2 & VCF0704==2
replace consistent = 0 if VCF0713==1 & VCF0704==2
replace consistent = 0 if VCF0713==3 & VCF0704==2
replace consistent = 0 if VCF0713==4 & VCF0704==2
replace consistent = 0 if VCF0713==9 & VCF0704==2
replace consistent = 0 if VCF0713==2 & VCF0704==1
replace consistent = 0 if VCF0713==3 & VCF0704==1
replace consistent = 0 if VCF0713==4 & VCF0704==1
replace consistent = 0 if VCF0713==9 & VCF0704==1

* Partisanship and partisan strength
gen pid = VCF0301
recode pid (0=.)
replace pid = . if pid==9
gen pidi = abs(pid-4) + 1

* Party
gen party = VCF0301
recode party (1=1) (2=1) (3=1) (5=0) (6=0) (7=0) (0=.) (4=2)
label define partylabel 0 "Republican" 1 "Democrat"  2 "Independent"
label values party partylabel  



* Ideological intensity
* ideology
gen ideology = VCF0803
recode ideology (9=.) (0=.)
gen ideoi = abs(ideology-4) + 1
replace ideoi = 0 if VCF0803==9

* Issues about policies directed to help Black Americans
gen aid_to_blacks = VCF0830	
recode aid_to_blacks (0=.) (9=.)

gen affirmative_action = VCF0867a		
recode affirmative_action (8=3) (7=3) (9=.)	

gen black_issues = (aid_to_blacks + affirmative_action) / 2



* Race (African American)
* Race
gen race = VCF0105b
recode race (0=.) (9=.)
label define racelabel 1 "White" 2 "Black" 3 "Hispanic" 4 "Other" 
label values race racelabel  
*1. White non-Hispanic
*2. Black non-Hispanic
*3. Hispanic
*4. Other or multiple races, non-Hispanic
gen black = 0 if race==1
replace black = 0 if race==3
replace black = 0 if race==4
replace black = 1 if race==2
label define blacklabel 1 "Bkack" 0 "Not black" 
label values race racelabel  


* Age
gen age = VCF0101
replace age = 97 if age>97

* Sex (female)
gen female = VCF0104
replace female = . if fem ==0
replace female = . if fem ==3
replace female = fem-1
label define femalelabel 0 "Male" 1 "Female"
label values female femalelabel  

* Education
gen education = VCF0110
replace edu = . if educ==0

* Income
gen income = VCF0114
replace income = . if income == 0

gen south = .
replace south = 0 if VCF0112 == 1
replace south = 0 if VCF0112 == 2
replace south = 0 if VCF0112 == 4
replace south = 1 if VCF0112 == 3
label var south "South Region Dummy"
label define southern 0 "0 Non-South" 1 "1 South"
label values south southern


* Interest
gen interest = VCF0310
recode interest (9=.) (0=.)

* Economic perceptions
gen natl_econ = VCF0871*-1+6

* Non-voters
gen turnout=VCF0703
recode turnou (3=1) (2=0) (1=0)

* Turnout and vote consistency
gen turnout_consistent = turnout
recode turnout_consistent (1=2)
replace turnout_consistent = 1 if vote_post!=vote & turnout==1

*Abstention and winning using candidate feeling thermometers
gen therm_dcandidate = VCF0424
recode therm_dcandidate (99=.) (98=.) (97=100)
gen therm_rcandidate = VCF0426
recode therm_rcandidate (99=.) (98=.) (97=100)
gen therm_dif = therm_rcandidate-therm_dcandidate
gen vote_abstain_therm = 1 if therm_dif>0 & turnout==0
replace  vote_abstain_therm = 0 if therm_dif<0 & turnout==0
* Non-voters who prefer Republican candidate are coded as 1
gen winner_abstained_therm = 1 if vote_abstain_therm == 1 & year==2004
replace winner_abstained_therm = 0 if vote_abstain_therm == 0 & year==2004
replace winner_abstained_therm = 1 if vote_abstain_therm == 0 & year==2008
replace winner_abstained_therm = 0 if vote_abstain_therm == 1 & year==2008
replace winner_abstained_therm = 1 if vote_abstain_therm == 0 & year==2012
replace winner_abstained_therm = 0 if vote_abstain_therm == 1 & year==2012
replace winner_abstained_therm = 1 if vote_abstain_therm == 1 & year==2016
replace winner_abstained_therm = 0 if vote_abstain_therm == 0 & year==2016

*Abstention and winning using partisanship
replace winner_abstained = 1 if turnout==0 & party==0 & year==2004
replace winner_abstained = 1 if turnout==0 & party==1 & year==2008
replace winner_abstained = 1 if turnout==0 & party==1 & year==2012
replace winner_abstained = 1 if turnout==0 & party==0 & year==2016
replace winner_abstained = . if party==2

* Voters and non-voters
gen winner_turnout = winner
replace winner_turnout = 2 if turnout==0


*********************************************
* Table 1 and predicted values for Figure 3 *
*********************************************
set more off
forval i = 2004(4)2016 {
reg satis c.racial_resentment##winner ideology interest ///
	natl_econ income age edu female if race==1 & year==`i' & vote_post==vote
margins , at(racial_resentment=(1(.25)5)) over(winner) post
}

***********
* Table 4 *
***********
set more off
forval i = 2004(4)2016 {
reg satis c.ideology##winner interest natl_econ income age edu female  ///
	if race==1 & year==`i' & vote_post==vote
reg satis c.diftherm##winner ideology interest natl_econ income age edu female  ///
	if race==1 & year==`i' & vote_post==vote
reg satis c.blackslazy##winner ideology interest natl_econ income age edu female  ///
	if race==1 & year==`i' & vote_post==vote
reg satis c.pid##winner ideology interest natl_econ income age edu female  ///
	if race==1 & year==`i' & vote_post==vote
}

*************** 
* Appendicies *
*************** 

*Figure A.1
drop if year<2004
# delimit ;
twoway
(hist turnout_consistent if race==1 & year>2003
	,
	by(year , col(4)) 
	percent 
	disc
	gap(10))
(function y = 0 if year>2003
	, 
	range(-.55 2.55) lcolor(black) )
	,
	aspect(1) 
	xlab(0 "Abstained" 1 "Inconsistent" 2 "Consistent" , angle(90)) 
	ylab(0 "0%" 25 "25%" 50 "50%" 75 "75%" , angle(0)) 
	subtitle(, fcolor(none) lcolor(none)) 
	ytitle(" ") 
	xtitle(" ")
;
# delimit cr

* Table B1 and predicted values for Figure B.1
forval i = 2004(4)2016 {
reg satis c.racial_resentment##winner_post ideology interest ///
	natl_econ income age edu female if race==1 & year==`i'	
margins , at(racial_resentment=(1(.25)5)) over(winner_post) post
}

* Table B.2 and predicted values for Figure B.1
set more off
forval i = 2004(4)2016 {
reg satis c.racial_resentment##winner_post ideology interest ///
	natl_econ income age edu female consistent if race==1 & year==`i'	
margins , at(racial_resentment=(1(.25)5)) over(winner_post) post
}

* Table B.3
reg satis c.racial_resentment##winner ideology natl_econ income age edu ///
female if race==1 & year==2008 & vote_post==vote

* Table B.10
forval i = 2004(4)2016 {
reg satisfaction c.winner##c.pid ideology natl_econ income age edu female ///
	pid if race==1 & year==`i' & vote_post==vote
}

* Table B.11
forval i = 2004(4)2016 {
reg satisfaction winner##i.party ideology natl_econ income age edu female ///
	interest if race==1 & year==`i' & vote_post==vote
}

* Table B.12
set more off
forval i = 2004(4)2016 {
reg satisfaction c.black_issues##winner ideology interest natl_econ income ///
	age edu female  if race==1 & year==`i' & vote_post==vote
}

* Table B.13
forval i = 2004(4)2016 {
reg satisfaction c.racial_resentment##winner ideology ///
	natl_econ income age edu female  interest  pid if race==1 & year==`i' & vote_post==vote
}

* Table B.14
forval i = 2004(4)2016 {
reg satisfaction c.racial_resentment##winner ideology ///
	natl_econ income age edu female  interest  i.party if race==1 & year==`i' & vote_post==vote
}

* Table B.15
forval i = 2004(4)2016 {
reg satisfaction  c.racial_resentment##winner##c.pidi ideology interest  ///
	natl_econ income age edu female if race==1 & year==`i' & vote_post==vote
}

* Table B.16
forval i = 2004(4)2016 {
reg satis c.racial_resentment##winner_abstained_therm ideology interest ///
	natl_econ income age edu female  if race==1 & year==`i'
}

* Table B.17
forval i = 2004(4)2016 {
reg satis c.racial_resentment##winner_abstained ideology interest ///
	natl_econ income age edu female  if race==1 & year==`i'
}

* Table B.18
set more off
forval i = 2004(4)2016 {
reg satis c.racial_resentment##winner_turnout ideology interest ///
	natl_econ income age edu female  if race==1 & year==`i'
}


* Table C.1 and predicted values for Figure C.1 and Figure C.2 and Figure C.4
forval i = 2004(4)2016 {
ologit satis c.racial_resentment##winner ideology interest ///
	natl_econ income age edu female if race==1 & year==`i' & vote_post==vote
margins , at(racial_resentment=(1(.25)5)) over(winner) post
}

* Table C.2 and predicted values for Figure C.2
forval i = 2004(4)2016 {
ologit satis c.racial_resentment##winner_post ideology interest ///
	natl_econ income age edu female if race==1 & year==`i'	
margins , at(racial_resentment=(1(.25)5)) over(winner_post) post
}

* Table C.3 and predicted values for Figure C.2
set more off
forval i = 2004(4)2016 {
ologit satis c.racial_resentment##winner_post ideology interest ///
	natl_econ income age edu female consistent if race==1 & year==`i'	
margins , at(racial_resentment=(1(.25)5)) over(winner_post) post
}

* Table C.4
ologit satis c.racial_resentment##winner ideology natl_econ income age edu ///
	female if race==1 & year==2008 & vote_post==vote
	
* Table C.8
forval i = 2004(4)2016 {
ologit satis c.ideology##winner interest natl_econ income age edu female  ///
	if race==1 & year==`i' & vote_post==vote
}

* Table C.9
forval i = 2004(4)2016 {
ologit satis c.diftherm##winner ideology interest natl_econ income age edu female  ///
	if race==1 & year==`i' & vote_post==vote
}

* Table C.10
forval i = 2004(4)2016 {
ologit satis c.blackslazy##winner ideology interest natl_econ income age edu female  ///
	if race==1 & year==`i' & vote_post==vote
}

* Table C.11
forval i = 2004(4)2016 {
ologit satis c.pid##winner ideology interest natl_econ income age edu female  ///
	if race==1 & year==`i' & vote_post==vote
}

* Table C.12
forval i = 2004(4)2016 {
ologit satis i.party##winner ideology interest natl_econ income age edu female  ///
	if race==1 & year==`i' & vote_post==vote
}

* Table C.13
forval i = 2004(4)2016 {
ologit satis c.black_issues##winner ideology interest natl_econ income age edu female  ///
	if race==1 & year==`i' & vote_post==vote
}

* Table C.14
forval i = 2004(4)2016 {
ologit satisfaction c.racial_resentment##winner ideology ///
	natl_econ income age edu female  interest  pid if race==1 & year==`i' & vote_post==vote
}

* Table C.15
forval i = 2004(4)2016 {
ologit satisfaction c.racial_resentment##winner ideology ///
	natl_econ income age edu female  interest i.party if race==1 & year==`i' & vote_post==vote
}

* Table C.16
forval i = 2004(4)2016 {
ologit satisfaction  c.racial_resentment##winner##c.pidi ideology interest  ///
	natl_econ income age edu female if race==1 & year==`i' & vote_post==vote
}

* Table C.17
forval i = 2004(4)2016 {
ologit satis c.racial_resentment##winner_abstained_therm ideology interest ///
	natl_econ income age edu female  interest  if race==1 & year==`i'
}

* Table C.18
forval i = 2004(4)2016 {
ologit satis c.racial_resentment##winner_abstained ideology interest ///
	natl_econ income age edu female  interest  if race==1 & year==`i'
}

* Table C.19
forval i = 2004(4)2016 {
ologit satis c.racial_resentment##winner_turnout ideology interest ///
	natl_econ income age edu female  interest  if race==1 & year==`i'
}

* Table C.20
gen rw = racial_resentment*winner
forval i = 2004(4)2016 {
quietly: ologit satis racial_resentment winner rw ideology interest ///
	natl_econ income age edu female   if race==1 & year==`i' & vote_post==vote
brant, detail
}

* Table C.21
gologit2 satis c.racial_resentment##winner ideology interest ///
	natl_econ income age edu female   ///
	if race==1 & year==2012 & vote_post==vote ,	autofit
